package com.easy.controller;

import com.easy.bean.Bed;
import com.easy.common.CommonResult;
import com.easy.dao.IBedDao;
import com.easy.service.IBedService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class BedController {



    @Autowired
    IBedService bedS;

    @Autowired
    IBedDao dao;



    @GetMapping("bednumber")
    public CommonResult getBednember() {
        List<Bed> da = bedS.getBednember();
        System.out.println( da); // 输出床位号码
        return CommonResult.success(da);
    }

    @GetMapping("bednumberbywid")
    public CommonResult getBednemerByWid(Bed bed) {
        List<Bed> da = bedS.getBednemerByWid(bed);
        System.out.println( da); // 输出床位号码
        return CommonResult.success(da);
    }
    @GetMapping("bed")
    public CommonResult getBed(Bed bed, Page page){
        List<Bed> list=bedS.getBed(bed,page);
        int count=bedS.getCount(bed);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);

    }
    @PostMapping("bed")
    public CommonResult addBedJSON(@RequestBody Bed bed){
        bedS.addBed(bed);
        bed=bedS.getBedById(bed.getId());
        return CommonResult.success(bed);
    }

    @DeleteMapping("bed/{id}")
    public CommonResult delBed(@PathVariable int id){
        int count=dao.delBed(id);
        if(count>0){
            return CommonResult.success();
        }else {
            return CommonResult.fail();
        }
    }
    @PutMapping("bed")
    public CommonResult editBedJSON(@RequestBody Bed bed ){
        bedS.editBed(bed);
        bed=bedS.getBedById(bed.getId());
        return CommonResult.success(bed);
    }



}
